home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 16010 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.4 KB  |  43 lines

  1. Path: ix.netcom.com!netnews
  2. From: giuliano@ix.netcom.com(Giuliano Carlini)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Virtual Inheritance
  5. Date: 8 Apr 1996 08:44:50 GMT
  6. Organization: Netcom
  7. Message-ID: <4kajm2$c8m@reader2.ix.netcom.com>
  8. References: <4k9tpv$173k@news-s01.ny.us.ibm.net>
  9. NNTP-Posting-Host: lbx-ca6-22.ix.netcom.com
  10. X-NETCOM-Date: Mon Apr 08  1:44:50 AM PDT 1996
  11.  
  12. In <4k9tpv$173k@news-s01.ny.us.ibm.net> bbogard@ibm.net writes: 
  13. >what is virtual inheritance? I have used ... VC 4.1 and BC 4.5 and
  14. >neither of those had virtual inheritance defined in the language.
  15. >What is it, and what is it used for?
  16.  
  17. Virtual inheritence is in both VC and BC. Consider the following:
  18.     class Object {
  19.         long    id;
  20.         ...
  21.     };
  22. Where every object in the system is identified by its id, and so
  23. there must be exactly one id per object. Now consider:
  24.     class Foo : public Object {...};
  25.     class Bar : public Object {...};
  26.     class FooBar: public Foo, Bar {...};
  27. A foobar has TWO id's. One inherited from foo and the other from
  28. bar. A memory layout for a foobar would be something like this:
  29.     Foo::Object::id
  30.     Foo::Object::...
  31.     Foo::...
  32.     Bar::Object::id
  33.     Bar::Object::...
  34.     Bar::...
  35.     FooBar::...
  36. So, how do we tell C++ to give us only one Object in a FooBar? Virtual
  37. inheritence!
  38.     class Foo : public virtual Object {...};
  39.     class Bar : public virtual Object {...};
  40.     class FooBar: public Foo, Bar {...};
  41.  
  42. giuliano
  43.